package br.ucb.GtTunningLog.server;

import java.util.Vector;

import br.ucb.GtTunningLog.client.GreetingService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {
	
	//O servidor pode ter uma coleção de posts. Que tal um vector??
	private Vector<String> postVector = new Vector<String>();
	
	
	public String greetServer(String input) {
		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");
		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}
	
	public String getPosts(){
		String allPosts = "";
		
		
		for(int i = 0; i < postVector.size(); i++){
			allPosts += postVector.elementAt(i);
		}
		
		allPosts = populateWithFakePosts();
		return allPosts;
		//return this.postVector;
	}
	
	private String createNewPost(String title, String formatedMessage, String owner, String subpost){
		
		String htmlHomeBody = "<div class=\"post\">" 
			+ "<h1 class=\"title\"><a href=\"#\">" + title + "</a></h1>"
			+ "<p class=\"meta\">Postado por <a href=\"#\">" + owner + "</a>em 28 de Maio de 2008"
			+ "&nbsp;&bull;&nbsp; <a href=\"#\" class=\"comments\">Comments (64)</a> &nbsp;&bull;&nbsp; <a href=\"#\" class=\"permalink\">Artigo Completo</a></p>"
			+ "<div class=\"entry\">"
			+ "<p>" + formatedMessage + "</p>";
			if(!"".equals(subpost) && subpost != ""){
				htmlHomeBody += "<p>" + subpost + "</p>";
			}
			
			htmlHomeBody += "</div>"
			+ "</div>";
		
		return htmlHomeBody;
	}	
	
	private String populateWithFakePosts(){
//		Post post = new Post("Flebinha Testando", 
//				"Post 1", "O Mestre", "Flebas");
		String str1 = createNewPost("Flebinha Testando", 
					"Post 1", "O Mestre", "Flebas");
		postVector.addElement(str1);
		str1 += createNewPost("Flebinha Testando", 
				"Post 2", "O Mestre", "Flebas 2");
		postVector.addElement(str1);
		
		return str1;
	}

	@Override
	public void addPost(String post) {
		this.postVector.addElement(post);
		
	}
}
